<template>
  <div class="container">
    <div class="login">
      <div style="margin-bottom: 40px; text-align: center">
        小李后台管理系统
      </div>
      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        :rules="rules"
        status-icon
        label-width="auto"
        class="demo-ruleForm"
      >
        <el-form-item prop="userName">
          <el-input v-model="ruleForm.userName" type="text" />
        </el-form-item>
        <el-form-item prop="passWord">
          <el-input v-model="ruleForm.passWord" type="password" />
        </el-form-item>

        <el-form-item prop="img">
          <el-input v-model="ruleForm.code" type="text" />
          <div class="login-code">
            <img @click="getCode" :src="img" class="login-code-img" />
          </div>
        </el-form-item>

        <el-form-item>
          <el-button
            size="large"
            type="primary"
            style="width: 100%"
            @click="submite(ruleFormRef)"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="footer">
      Copyright © 2018-2024 ruoyi.vip All Rights Reserved.
    </div>
  </div>
</template>

<script setup>


import { onMounted, ref,getCurrentInstance } from "vue";
import { captchaImage } from "@/api/login/login.js";
import { useLoginStore } from "@/store/login/login.js";

import { useRouter } from "vue-router";
const router = useRouter();
import { useToken } from "@/untils/token.js";
let loginStore = useLoginStore();
let { proxy } = getCurrentInstance();

let ruleForm = ref({
  passWord: "admin123",
  userName: "admin",
  code: "",
  uuid: "",
});

let ruleFormRef = ref();

const rules = ref({
  userName: [
    { required: true, message: "请输入账号密码", trigger: "blur" },
    { min: 3, max: 5, message: "Length should be 3 to 5", trigger: "blur" },
  ],
  passWord: [
    {
      required: true,
      message: "Please input Activity name",
      trigger: "blur",
    },
    { min: 1, max: 20, message: "Length should be 3 to 5", trigger: "blur" },
  ],
});

const submite = async (form) => {
  if (!ruleFormRef.value) return;
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      loginStore
        .login(
          ruleForm.value.userName,
          ruleForm.value.passWord,
          ruleForm.value.code,
          ruleForm.value.uuid
        )
        .then((res) => {
          console.log(res);
          useToken().addToken(res.token);
          //登陆成功之后弹窗提示登陆成功
          proxy.$msg.elSucces(res.msg)
          //判断有没有需要跳转的路由参数 如果有就跳转
          if (router.currentRoute.value.query.redirect) {
            router.replace({
              path: router.currentRoute.value.query.redirect,
            });
          }else{
            router.replace("/home");
          }
        }).catch((e)=>{
          console.log("登录失败",e);
        })

      //在这里进行路由跳转
    } else {
      console.log("error submit!", fields);
    }
  });
};
let img = ref("");
onMounted(() => {
  getCode();
});

let getCode = () => {
  captchaImage().then((res) => {
    img.value = "data:image/gif;base64," + res.img;
    ruleForm.value.uuid = res.uuid;
    console.log(img.value);
  });
};
</script>

<style lang="scss" scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("@/assets/login/login-background.jpg");
  background-size: cover;
  .login {
    width: 300px;
    height: 300px;
    padding: 50px 25px 50px 25px;
    background-color: white;
    border-radius: 10px;
    margin: 50px 25px 50px 25px;
  }
  .footer {
    position: fixed;
    bottom: 0px;
    margin-bottom: 50px;
    color: #fff;
  }
}

.login-code-img {
  height: 40px;
  width: 100px;
  padding-left: 12px;
}
</style>
